﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;


namespace DomainModel.Concrete
{
    class SqlStoresRepository : IStoresRepository
    {
        private Table<Store> storesTable;

        public SqlStoresRepository(string connectionString)
        {
            storesTable = new DataContext(connectionString).GetTable<Store>();
        }

        public IQueryable<Store> Stores
        {
            get { return storesTable; }
        }

        public void SaveStore(Store store)
        {
            if (store.StoreID == 0)
            {
                //It's a new store
                storesTable.InsertOnSubmit(store);
            }
            else
            {
                //It's an existing store
                storesTable.Attach(store);

                //Tell data context to detect any changes since last save
                storesTable.Context.Refresh(RefreshMode.KeepCurrentValues, store);
            }

            storesTable.Context.SubmitChanges();
        }

        public void DeleteStore(Store store)
        {
            storesTable.DeleteOnSubmit(store);
            storesTable.Context.SubmitChanges();
        }
    }
}
